// pages/user-login/user-login.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    localImg: app.globalData.staticimgUrl,  // 本地图片
    phone: '', //电话号码
    verifyCode: '', //验证码
    codename: '获取验证码',
    disabled: false,
    recommendUser: '', // 扫码结果
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  getPhoneValue: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getverifyCode: function(e) {
    this.setData({
      verifyCode: e.detail.value
    })
  },
  // 获取验证码
  getCode: function() {
    var phone = this.data.phone;
    var that = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-8])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      var num = 4;
      var timer = setInterval(function() {
        num--;
        if (num <= 0) {
          clearInterval(timer);
          that.setData({
            codename: '重新发送',
            disabled: false
          })
        } else {
          that.setData({
            codename: num + "s"
          })
        }
      }, 1000)
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      var num = 4;
      var timer = setInterval(function() {
        num--;
        if (num <= 0) {
          clearInterval(timer);
          that.setData({
            codename: '重新发送',
            disabled: false
          })
        } else {
          that.setData({
            codename: num + "s"
          })
        }
      }, 1000)
      return false;
    } else {
      app.request({
        url: 'system/mobile/sendCode',
        data: {
          'mobile': phone,
          'isDebug': 1
        },
        success(res) {
          // console.log(res.message)
          if (res.data.code == 200) {
            wx.showToast({
              title: '获取验证码成功',
              icon: 'none',
              duration: 1000
            })
          } else if (res.data.code == 4001) {
            wx.showToast({
              title: '短信验证码发送失败',
              icon: 'none',
              duration: 2000
            })
          }else if (res.data.code == 4002){
            wx.showToast({
              title: '当天发送验证码次数已达上限',
              icon: 'none',
              duration: 2000
            })
          } else if (res.data.code == 4003) {
            wx.showToast({
              title: '一小时内发送验证码次数已达上限',
              icon: 'none',
              duration: 2000
            })
          } else if (res.data.code == 4004) {
            wx.showToast({
              title: '一分钟内发送验证码次数已达上限',
              icon: 'none',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none',
              duration: 1000
            })
          }
          var num = 61;
          var timer = setInterval(function() {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codename: '重新发送',
                disabled: false
              })
            } else {
              that.setData({
                codename: num + "s"
              })
            }
          }, 1000)
        }
      })
    }
  },
  //获取验证码
  getVerificationCode() {
    this.getCode();
    var that = this
    that.setData({
      disabled: true
    })
  },
  // 扫码登入
  scanLogin() {
    const that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        that.setData({
          recommendUser: res.result
        })

      }
    })
  },
  // 登入
  logined: function(e) {
    var that = this;
    var phone = that.data.phone;
    var verifyCode = that.data.verifyCode;
    var userInfo = wx.getStorageSync('userInfo');
    var openid = wx.getStorageSync('openid');

    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-8])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
     
      app.request({
        url: 'doctor/member/reg',
        data: {
          'mobile': phone,
          'verifyCode': verifyCode,
          'roleType': 2,
          'openId': openid,
          'avatarUrl': userInfo.avatarUrl,
          'nickName': userInfo.nickName,
          'gender': userInfo.gender,
          'city': userInfo.city,
          'recommendUser': that.data.recommendUser
        },
        success(res) {
            var toast = res.data[0].msg;
            wx.showToast({
              title: toast,
            });
            wx.setStorageSync("token", res.data[0].token);
            wx.setStorageSync("userid", res.data[0].userId);
            wx.setStorageSync("auditState", res.data[0].auditState);
            wx.setStorageSync("isLogin", 1);
            app.globalData.token = res.data[0].token;
            if (res.data[0].perfect == 1 || res.data[0].auditState == 3) {
              wx.redirectTo({
                url: "../doctor-authentication-1/doctor-authentication-1"
              })
            } else {
              if (res.data[0].auditState == 1) {
                wx.setStorageSync("auditState", 1);
                wx.navigateTo({
                  url: '../doctor-authentication-3/doctor-authentication-3',
                })
              } else if (res.data[0].auditState == 2) {
                //审核通过
                wx.setStorageSync("auditState", 2);
                wx.reLaunch({
                  url: '../doctor-index/doctor-index',
                })
              } else if (res.data[0].auditState == 3) {
                //审核未通过
                wx.setStorageSync("auditState", 3);
                return false;
              }
            }
            app.request({
              url: 'doctor/memberTaskApi/addBeauty',
              data: {
                'type': 1,
                'userId': res.data[0].userId,
              },
              success(res) {}
            })
        },
        fail: function (res) {
          if(res.status_code == 4002){
            wx.showToast({
              title: "请输入正确短信验证码",
              icon: 'none'
            });
          } else if (res.status_code == 4005) {
            wx.showToast({
              title: "账号被禁用",
              icon: 'none'
            });
          } else if (res.status_code == 4006) {
            wx.showToast({
              title: "账号已注销",
              icon: 'none'
            });
          } else if (res.status_code == 4007) {
            wx.showToast({
              title: "账号不存在",
              icon: 'none'
            });
          } else if (res.status_code == 4009 || res.status_code == 4010 || res.status_code == 4011){
            wx.showToast({
              title: "微信账号与手机号不匹配",
              icon: 'none'
            });
          } else if (res.status_code == 300) {
            wx.showToast({
              title: "请求失败",
              icon: 'none'
            });
          } else{
            wx.showToast({
              title: res.data.message + res.status_code,
              icon: 'none'
            });
          }
         
        }
      })
    }
  }
})